# Oracle客户端环境检查脚本
# 用于诊断DMZ服务器上的Oracle客户端配置问题

Write-Host "=== Oracle客户端环境检查 ===" -ForegroundColor Green
Write-Host ""

# 1. 检查Oracle客户端安装路径
Write-Host "1. 检查Oracle客户端安装路径:" -ForegroundColor Yellow
$oracleHomes = @(
    "D:\app\Administrator\product\11.2.0\client_1",
    "D:\app\Administrator\product\11.2.0\client_2"
)

foreach ($home in $oracleHomes) {
    if (Test-Path $home) {
        Write-Host "   ✓ 找到: $home" -ForegroundColor Green
        
        # 检查关键文件
        $oracleDll = Join-Path $home "bin\oci.dll"
        $tnsAdmin = Join-Path $home "network\admin"
        
        if (Test-Path $oracleDll) {
            Write-Host "     - oci.dll: 存在" -ForegroundColor Green
        } else {
            Write-Host "     - oci.dll: 缺失" -ForegroundColor Red
        }
        
        if (Test-Path $tnsAdmin) {
            Write-Host "     - network/admin: 存在" -ForegroundColor Green
        } else {
            Write-Host "     - network/admin: 缺失" -ForegroundColor Red
        }
    } else {
        Write-Host "   ✗ 未找到: $home" -ForegroundColor Red
    }
}

Write-Host ""

# 2. 检查环境变量
Write-Host "2. 检查Oracle环境变量:" -ForegroundColor Yellow

$envVars = @(
    "ORACLE_HOME",
    "TNS_ADMIN",
    "PATH"
)

foreach ($var in $envVars) {
    $value = [Environment]::GetEnvironmentVariable($var, "Machine")
    if ($value) {
        Write-Host "   $var (系统): $value" -ForegroundColor Green
    } else {
        Write-Host "   $var (系统): 未设置" -ForegroundColor Yellow
    }
    
    $userValue = [Environment]::GetEnvironmentVariable($var, "User")
    if ($userValue) {
        Write-Host "   $var (用户): $userValue" -ForegroundColor Green
    }
}

Write-Host ""

# 3. 检查PATH中的Oracle路径
Write-Host "3. 检查PATH中的Oracle相关路径:" -ForegroundColor Yellow
$pathVar = $env:PATH
$oraclePaths = $pathVar -split ';' | Where-Object { $_ -like '*oracle*' -or $_ -like '*client*' }

if ($oraclePaths) {
    foreach ($path in $oraclePaths) {
        if (Test-Path $path) {
            Write-Host "   ✓ $path" -ForegroundColor Green
        } else {
            Write-Host "   ✗ $path (路径不存在)" -ForegroundColor Red
        }
    }
} else {
    Write-Host "   未在PATH中找到Oracle相关路径" -ForegroundColor Yellow
}

Write-Host ""

# 4. 检查.NET应用程序的位数
Write-Host "4. 检查.NET应用程序架构:" -ForegroundColor Yellow
try {
    $processInfo = Get-Process -Name "Lzfy_His_Service" -ErrorAction SilentlyContinue
    if ($processInfo) {
        $is64Bit = [Environment]::Is64BitProcess
        Write-Host "   当前进程是64位: $is64Bit" -ForegroundColor Green
        
        if ($is64Bit) {
            Write-Host "   建议使用64位Oracle客户端 (client_2)" -ForegroundColor Cyan
        } else {
            Write-Host "   建议使用32位Oracle客户端 (client_1)" -ForegroundColor Cyan
        }
    } else {
        Write-Host "   服务未运行，无法检查进程架构" -ForegroundColor Yellow
    }
} catch {
    Write-Host "   无法检查进程架构: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host ""

# 5. 检查Oracle客户端版本
Write-Host "5. 检查Oracle客户端版本:" -ForegroundColor Yellow
foreach ($home in $oracleHomes) {
    if (Test-Path $home) {
        $sqlplusPath = Join-Path $home "bin\sqlplus.exe"
        if (Test-Path $sqlplusPath) {
            try {
                $versionOutput = & $sqlplusPath -version 2>&1
                Write-Host "   $home 版本:" -ForegroundColor Green
                Write-Host "   $versionOutput" -ForegroundColor White
            } catch {
                Write-Host "   无法获取 $home 的版本信息" -ForegroundColor Red
            }
        }
    }
}

Write-Host ""

# 6. 生成建议的环境变量配置
Write-Host "6. 建议的环境变量配置:" -ForegroundColor Yellow

# 检查哪个客户端更合适
$recommendedClient = "D:\app\Administrator\product\11.2.0\client_2"  # 默认推荐64位

if ([Environment]::Is64BitOperatingSystem) {
    Write-Host "   检测到64位操作系统，推荐使用64位客户端" -ForegroundColor Cyan
    $recommendedClient = "D:\app\Administrator\product\11.2.0\client_2"
} else {
    Write-Host "   检测到32位操作系统，推荐使用32位客户端" -ForegroundColor Cyan
    $recommendedClient = "D:\app\Administrator\product\11.2.0\client_1"
}

Write-Host ""
Write-Host "   建议设置以下环境变量:" -ForegroundColor Green
Write-Host "   ORACLE_HOME=$recommendedClient" -ForegroundColor White
Write-Host "   TNS_ADMIN=$recommendedClient\network\admin" -ForegroundColor White
Write-Host "   PATH中添加: $recommendedClient\bin" -ForegroundColor White

Write-Host ""

# 7. 测试tnsping
Write-Host "7. 测试TNS连接:" -ForegroundColor Yellow
foreach ($home in $oracleHomes) {
    if (Test-Path $home) {
        $tnspingPath = Join-Path $home "bin\tnsping.exe"
        if (Test-Path $tnspingPath) {
            Write-Host "   使用 $home 测试连接:" -ForegroundColor Green
            try {
                $env:ORACLE_HOME = $home
                $env:TNS_ADMIN = Join-Path $home "network\admin"
                $result = & $tnspingPath "10.3.255.250:15521/HISDB" 2>&1
                Write-Host "   $result" -ForegroundColor White
            } catch {
                Write-Host "   tnsping测试失败: $($_.Exception.Message)" -ForegroundColor Red
            }
        }
    }
}

Write-Host ""
Write-Host "=== 检查完成 ===" -ForegroundColor Green
Write-Host ""
Write-Host "如果发现问题，请按照建议配置环境变量后重启服务" -ForegroundColor Cyan